package com.chilli.cosima.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.chilli.cosima.common.page.PagerBean;
import com.opensymphony.xwork2.ActionSupport;

public class BaseSupport extends ActionSupport implements ServletRequestAware
{
	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = -4864002091802028657L;

	protected final Log logger = LogFactory.getLog(getClass());

	protected int pageSize = 10;

	protected HttpServletRequest request;

	protected static final String JUMP_PAGE = "jumppage";

	protected static final String DETAIL = "detail";
	
	protected static final int BUFFER_SIZE = 16 * 1024;
	
	protected static String getExtention(String fileName)
	{
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}

	protected static void copy(File src, File dst)
	{
		try
		{
			InputStream in = null;
			OutputStream out = null;
			try
			{
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0)
				{
					out.write(buffer);
				}
			} finally
			{
				if (null != in)
				{
					in.close();
				}
				if (null != out)
				{
					out.close();
				}
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	protected final void setPager(List list, String action, int count)
	{
		PagerBean pagerBean = new PagerBean();
		String page = request.getParameter("page");
		String currPage = "0";
		try
		{
			// 如果产生异常则表示输入非法
			if (null != page && !"".equals(page))
			{
				currPage = page;
			}
		} catch (NumberFormatException e)
		{
			logger.info("paramater invalid. format " + currPage);
			currPage = null;
		}

		// 如果记录集为空

		if (count == 0)
		{
			pagerBean.setCurrentPage(Integer.valueOf(1));
			pagerBean.setNextPage(Integer.valueOf(1));
			pagerBean.setPreviousPage(Integer.valueOf(1));
			pagerBean.setTotal(Integer.valueOf(1));
			pagerBean.setLastPage(Integer.valueOf(1));
		} else
		{
			// 总页数

			int totalPage = count / pageSize;

			if (count % pageSize != 0)
			{
				totalPage += 1;
			}

			// 如果没有输入页数则当前也和上一页设置为第一页

			if ((null == currPage) || ("".equals(currPage)))
			{
				pagerBean.setCurrentPage(Integer.valueOf(1));
				pagerBean.setPreviousPage(Integer.valueOf(1));

				// 如果只有一页

				if (totalPage == 1)
				{
					pagerBean.setNextPage(Integer.valueOf(1));
				} else
				{
					pagerBean.setNextPage(Integer.valueOf(2));
				}
				pagerBean.setLastPage(Integer.valueOf(totalPage));
			} else
			{
				// 当前页数
				int cPage = Integer.parseInt(currPage);

				if (cPage > totalPage)
				{
					cPage = totalPage;
				}
				if (cPage <= 1)
				{
					cPage = 1;
				}

				// pagerBean.setCurrentPage(Integer.valueOf(currPage));
				pagerBean.setCurrentPage(cPage);

				if ((cPage - 1) <= 0)
				{
					pagerBean.setPreviousPage(Integer.valueOf(1));
				} else
				{
					pagerBean.setPreviousPage(cPage - 1);
				}

				if (cPage + 1 >= totalPage)
				{
					pagerBean.setNextPage(totalPage);
				} else
				{
					pagerBean.setNextPage(cPage + 1);
				}
			}

			pagerBean.setLastPage(Integer.valueOf(totalPage));
			pagerBean.setTotal(Integer.valueOf(totalPage));
		}
		pagerBean.setValueList(list);
		pagerBean.setAction(action);
		request.setAttribute("pagerBean", pagerBean);
	}

	public void setServletRequest(HttpServletRequest request)
	{
		this.request = request;
	}

}
